(function ($) {
    $.fn.readjdbc = function (method, param) {
        if (typeof method == "string") {
            return $.fn.readjdbc.methods[method](this, param);
        }
        param = method || {};
        return this.each(function () {
            //全局参数
            var data = $.data(this, "readjdbc");
            if (data) {
                $.extend(data.options, param);
            } else {
                let options = $.extend({}, $.fn.readjdbc.defaults, $.fn.readjdbc.parseOptions(this), param);
                $.data(this, "readjdbc", {options: options});
                /*$(this).bind("click",function(e,p){
                    console.log('step click');
                    return false;
                }).bind("dbclick",function(e,p){
                    console.log('step dbclicked');
                    return false;
                });*/
            }
            create(this);
        });


    };

    function create(e) {
        let options = $.data(e, 'readjdbc').options;
        let that = $(e).empty()
            .addClass('unit-readjdbc')
            .attr("id", options.id || "readjdbc_" + options.stepId)
            .window({
                width: 800,
                height: 500,
                id: options.id,
                title: '表输入',
                iconCls: 'icon-read-jdbc',
                collapsible: false,
                minimizable: false,
                maximizable: false,
                onOpen: function () {
                    let that = this;
                    let ff = $('<form></form>').appendTo(e).form({});
                    $('<div class="step-form-item"><input id="describe"></div>').appendTo(ff)
                        .children('input')
                        .textbox({
                            width: 600,
                            label: '步骤名称',
                            value: options.describe,
                            labelWidth: 120,
                            labelPosition: 'before',
                            labelAlign: 'right',
                            multiline: false,
                            readonly: true
                        });
                    $('<div class="step-form-item"><input id="ds"></div>').appendTo(ff)
                        .children('input')
                        .textbox({
                            width: 600,
                            label: '数据库连接',
                            labelWidth: 120,
                            labelPosition: 'before',
                            labelAlign: 'right',
                            multiline: false
                        });
                    /*$('<div class="step-form-item"><input name="tableName"></div>')
                        .appendTo(ff)
                        .children('input')
                        .textbox({
                            width: 600,
                            label: '表名',
                            labelWidth: 120,
                            labelPosition: 'before',
                            labelAlign: 'right',
                            multiline: false
                        });*/
                    $('<div class="step-form-item"><input id="executeSql"></div>')
                        .appendTo(ff)
                        .children('input')
                        .textbox({
                            width: 600,
                            height: 200,
                            label: 'SQL',
                            labelWidth: 120,
                            labelPosition: 'before',
                            labelAlign: 'right',
                            multiline: true,
                        });

                    loadData(that);
                },
            });
    }

    function loadData(e) {
        var options = $.data(e, 'readjdbc').options;
        $.get($.rootUrl +options.url+ "/" + options.unitType, {stepId: options.stepId}, function (res) {
            if (res.success) {
                let result = res.result;
                $.data(e, 'readjdbc', $.extend($.data(e, 'readjdbc'), {data: result}));
                $.each(options.columns, function (i, columnName) {
                    if($('#'+columnName).length){
                        console.log($('#'+columnName))
                        $('#'+columnName).textbox('setValue', result[columnName]);
                    }
                });
            } else {
                $.messager.alert("警告",res.message);
            }
        });
    }

    $.fn.readjdbc.defaults = {
        id: null,
        title: null,
        iconCls: null,
        columns: ['stepId', 'ds', 'tableName', 'executeSql'],
        url:'/zjk/convert/stepUnit',
        stepId: null,
        describe: null,
        unitType: null,
        ds: null,
        tableName: null,
        sql: null
    };

    $.fn.readjdbc.methods = {
        options: function (jq) {
            return $.data(jq[0], "readjdbc").options;
        }, readjdbc: function (jq) {
            return $.data(jq[0], "readjdbc").readjdbc;
        }, open: function (jq) {
            return jq.window('open')
        }
    };
    $.fn.readjdbc.parseOptions = function (selector) {
        var options = $.extend(
            {},
            $.parser.parseOptions(
                selector,
                ["id", "title", "iconCls","stepId", "describe", "unitType"]
            )
        );
        return options;
    };


})(jQuery);
